package com.CST.controller.zx;

import com.CST.common.api.ResultObject;
import com.CST.dto.cb.DateDTO;
import com.CST.dto.zx.DeptBalanceDetailDTO;
import com.CST.dto.zx.EndingBalanceDTO;
import com.CST.dto.zx.PrimusCashDTO;
import com.CST.dto.zx.ZxDeptDirectorFundDTO;
import com.CST.entity.zx.ZX_PrimusCash;
import com.CST.service.zx.ZxDeptDirectorFundService;
import com.CST.util.PageHelperTool;
import com.CST.vo.zx.AllDeptVO;
import com.CST.vo.zx.DeptBalanceDetailVO;
import com.CST.vo.zx.EndingBalanceVO;
import com.CST.vo.zx.PrimusCashVO;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author xnd
 * @since 2023-05-05
 */
@Api(tags = {"科主任基金"})
@RestController
@RequestMapping("/zxDeptDirectorFund")
public class ZxDeptDirectorFundController {

    @Autowired
    private ZxDeptDirectorFundService zxDeptDirectorFundService;

    @PostMapping("/gatherDeptDirectorInfo")
    @ApiOperation("采集科主任基金")
    public ResultObject<String> gatherDeptDirectorInfo(@RequestBody ZxDeptDirectorFundDTO dto) {
        String result = zxDeptDirectorFundService.gatherDeptDirectorInfo(dto);
        if (!"success".equals(result)) {
            return ResultObject.success(result, "采集失败");
        }
        return ResultObject.success("采集成功");
    }

    /**
     * 这个是财务处他要多开一个页面，里面只要科室名称，科室编码，跟期末余额
     * @param dto
     * @return
     */
    @PostMapping("/listDeptDirectorFundBalance")
    @ApiOperation("科室汇总数据")
    public ResultObject<PageInfo<EndingBalanceVO>> listDeptDirectorFundBalance(@RequestBody EndingBalanceDTO dto) {
        List<EndingBalanceVO> endingBalanceVOS = zxDeptDirectorFundService.listDeptDirectorFundBalance(dto);
        if (endingBalanceVOS.isEmpty()) {
            throw new RuntimeException("科室汇总数据为空");
        }
        PageInfo<EndingBalanceVO> resultList = PageHelperTool.excutePageInfo(endingBalanceVOS,
                dto.getNum(),
                dto.getSize());
        return ResultObject.success(resultList);
    }

    @PostMapping("/download")
    @ApiOperation("科主任基金汇总导出")
    public void exportExcel3(HttpServletResponse response, @RequestBody  EndingBalanceDTO dto) throws IOException {
        zxDeptDirectorFundService.download(response,dto);
    }

    @PostMapping("/downloadDept")
    @ApiOperation("科主任基金科室汇总导出")
    public void downloadDept(HttpServletResponse response, @RequestBody  EndingBalanceDTO dto) throws IOException {
        zxDeptDirectorFundService.downloadDept(response,dto);
    }

    @PostMapping("/DeptDirectorFundBalanceByCode")
    @ApiOperation("科室查询明细")
    public ResultObject<List<DeptBalanceDetailVO>> DeptDirectorFundBalanceByCode(
            @RequestBody DeptBalanceDetailDTO dto) {
        return ResultObject.success(zxDeptDirectorFundService.DeptDirectorFundBalanceByCode(dto));
    }


    @PostMapping("/downloadE")
    @ApiOperation("科主任基金明细导出")
    public void exportExcel4(HttpServletResponse response, @RequestBody  DeptBalanceDetailDTO dto) throws IOException {
        zxDeptDirectorFundService.downloadE(response,dto);
    }

    @GetMapping("/listAllDept")
    @ApiOperation("科主任基金科室下拉框")
    public ResultObject<List<AllDeptVO>> listDept() {
        return ResultObject.success(zxDeptDirectorFundService.listDept());
    }

    @PostMapping("/listPrimusCash")
    @ApiOperation("获取科主任基金2019期初设置数据")
    public ResultObject<PageInfo<PrimusCashVO>> listPrimusCash(@RequestBody PrimusCashDTO entity) {
            List<PrimusCashVO> result =zxDeptDirectorFundService.listPrimusCash(entity);
        PageInfo<PrimusCashVO> resultList = PageHelperTool.excutePageInfo(result,
                entity.getPageIndex(),
                entity.getPageSize());
        return ResultObject.success(resultList);
    }

    @PostMapping("/saveOrUpdate")
    @ApiOperation("添加/修改科主任基金2019期初设置数据")
    public ResultObject saveOrUpdate(@RequestBody ZX_PrimusCash entity) {
        return ResultObject.success(zxDeptDirectorFundService.saveOrUpdate(entity));
    }

    @GetMapping("/deletePrimusCash")
    @ApiOperation("删除科主任基金记录")
    public ResultObject deletePrimusCash(Integer id) {
        return ResultObject.success(zxDeptDirectorFundService.deletePrimusCash(id));
    }

}
